<?php
class Core_Model_Area {
	
	protected $_areaCode;
	
	protected $_themes = null;
	
	public function getCurrentTheme() {
		$theme = Mag::getConfig()->get($this->getName() . '/theme');
		return $this->getTheme($theme);
	}
	
	public function getAvailableThemes() {
		if (!$this->_themes) {
			$themePaths = glob($this->getAreaDir() . DS . '*', GLOB_ONLYDIR);
			foreach($themePaths as $path) {
				$name = basename($path);
				$config = require($path . DS . 'config.php');
				$config['name'] = $name;
				
				$theme = Mag::object('Core_Model_Theme', $this, $config);
				$this->_themes[$name] = $theme;
			}
		}
		return $this->_themes;
	}
	
	public function getTheme($name) {
		$themes = $this->getAvailableThemes();
		return isset($themes[$name]) ? $themes[$name] : null;
	}
	
	public function activeTheme($name) {
		// set config node: area.theme
		// save config to db
	}
	
	public function getAreaDir() {
		return BP . DS . 'design' . DS . $this->getName();
	}
	
	public function getName() {
		return $this->_areaCode;
	}
}